


package com.embedsky.administrator.mycardreader;

import android.text.TextUtils;
import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.embedsky.administrator.mycardreader.message.SocketMessage;
import com.embedsky.administrator.mycardreader.socket.ClientManager;
import com.embedsky.administrator.mycardreader.socket.IClientListener;
import com.embedsky.administrator.mycardreader.socket.IClientManager;

import java.util.ArrayList;
import java.util.List;

/**
 * @Des:
 * @Author: hang.zhao
 * @Date: 2023/2/28
 */
public class MainViewModel extends ViewModel {

    private String TAG = MainViewModel.class.getSimpleName();
    private IClientManager clientManager;
    private List<SocketMessage> socketMessageList = new ArrayList<>();
    private MutableLiveData<List<SocketMessage>> _messageList = new MutableLiveData<>();
    public LiveData<List<SocketMessage>> messageList = _messageList;


    public void setMessageList(List<SocketMessage> messageList) {
        _messageList.postValue(messageList);
    }

    public void sendData(String data) {
        SocketMessage message = new SocketMessage();
        message.setData(data);
        message.setType(SocketMessage.MESSAGE_SEND);
        socketMessageList.add(message);
        clientManager.sendData(data);
        _messageList.postValue(socketMessageList);
    }

    public List<SocketMessage> getSocketMessageList() {
        return _messageList.getValue();
    }

    public void stopClient() {
        if (clientManager != null) {
            clientManager.finish();
        }
    }


    public void connectSocket(String ip) {
        Log.d(TAG, "connectSocket: " + ip);
        //ip = "192.168.49.1";
        if (!TextUtils.isEmpty(ip)) {

            clientManager = new ClientManager(ip, 8848, new IClientListener() {
                @Override
                public void onConnected(String ip, int port) {
                    //clientManager.sendData("hello server");
                    Log.d(TAG, "onConnected: success");
                }

                @Override
                public void onConnectedFail(String ip, int port) {

                }

                @Override
                public void parseData(byte[] bytes, String ip) {
                    String message = new String(bytes);
                    SocketMessage socketMessage = new SocketMessage();
                    socketMessage.setData(message);
                    socketMessage.setType(SocketMessage.MESSAGE_RECEIVE);
                    socketMessageList.add(socketMessage);
                    //runOnUiThread(() -> showToast("收到服务端响应消息:" + message));
                    Log.d(TAG, "parseData: " + message);
                    _messageList.postValue(socketMessageList);
                }

                @Override
                public void onServerDisconnect(String ip) {
                    stopClient();
                }

                @Override
                public void onFinish() {

                }
            });
        } else {
            //showToast("IP为空");
        }
    }
}
